package edu.cornell.cs4740.postag.parsing;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ParseFile {
  public static final String sentenceMarker = "<s>";
  
  public static List<Sentence> parseTrainingFile(String filepath)
      throws Exception {
    ArrayList<Sentence> listOfSentences = new ArrayList<Sentence>();
    ArrayList<WordContainer> words = new ArrayList<WordContainer>();

    FileInputStream fstream = new FileInputStream(filepath);
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;

    while ((strLine = br.readLine()) != null) {
      // pos word
      String[] split = strLine.split(" ");
      if (split[0].equals(sentenceMarker) && words.size() > 0) {
        listOfSentences.add(new Sentence(words));
        words = new ArrayList<WordContainer>();
      } else if (!split[0].equals(sentenceMarker)) {
        words.add(new WordContainer(split[1], split[0]));
      }
    }
    
    listOfSentences.add(new Sentence(words));
    
    in.close();

    return listOfSentences;
  }

  public static List<Sentence> parseTestFile(String filepath)
      throws Exception {
    ArrayList<Sentence> listOfSentences = new ArrayList<Sentence>();
    ArrayList<WordContainer> words = new ArrayList<WordContainer>();

    FileInputStream fstream = new FileInputStream(filepath);
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;

    while ((strLine = br.readLine()) != null) {
      if (strLine.equals(sentenceMarker) && words.size() > 0) {
        listOfSentences.add(new Sentence(words));
        words = new ArrayList<WordContainer>();
      } else if (!strLine.equals(sentenceMarker)){
        words.add(new WordContainer(strLine, null));
      }
    }
    
    listOfSentences.add(new Sentence(words));
    
    in.close();

    return listOfSentences;
  }
}